package com.example.demo.controller;

import com.example.demo.service.CommodityService;
import com.example.demo.util.Commodity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;

@RestController
@RequestMapping("/commodity")
public class CommodityController {

    @Autowired
    CommodityService commodityService;

    @GetMapping("/{id}")
    public ResponseEntity<Commodity> getCommodityById(@PathVariable int id) {
        // 实际开发中应调用service层获取数据，此处模拟返回
        Commodity commodity = commodityService.getCommodityById(id);
        return ResponseEntity.ok(commodity);
    }

    // 新增商品
    @PostMapping("/add")
    public ResponseEntity<String> addCommodity(@RequestBody Commodity commodity) {
        // 此处应调用service层保存商品
        commodityService.addCommodity(commodity);
        return ResponseEntity.ok("商品添加成功，ID:" + commodity.getId());
    }

    // 删除商品
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> deleteCommodity(@PathVariable Integer id) {
        commodityService.deleteCommodity(id);
        return ResponseEntity.ok("ID为" + id + "的商品已删除");
    }

    // 更新商品
    @PutMapping("/update")
    public ResponseEntity<Commodity> updateCommodity(@RequestBody Commodity commodity) {
        //此处应调用service层更新商品
        commodity.setUpdateTime(new Date());  // 模拟更新时间修改
        commodityService.updateCommodity(commodity);
        return ResponseEntity.ok(commodity);
    }

    // 获取所有商品列表
    @GetMapping("/list")
    public ResponseEntity<List<Commodity>> getAllCommodities() {
        commodityService.getList();
        return ResponseEntity.ok(new ArrayList<>());
    }
}